package swing;

import javax.swing.JPanel;
import java.awt.FlowLayout;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;

import pojo.Website;

import java.awt.BorderLayout;
import net.miginfocom.swing.MigLayout;
import javax.swing.border.LineBorder;
import java.awt.Color;
import javax.swing.JButton;

import dao.WebsiteDAO;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class WebInformation extends JPanel {
	private JTextField link;
	private JTextField menu1;
	private JTextField menu2;
	private JTextField webContent;
	private JTextField page;
	private JTextField image;
	private JTextField bookInfo;
	private JTextField bookContent;
	private JTextField webType;
	private JLabel lbID; 
	private JButton btnAdd; 
	private JButton btnUpdate;
	private Website myWeb=null;
	private JButton btnChoose;
	private WebsiteGUI webGUI;
	private static int iMenu1=1;
	private static int iMenu2=2;
	private static int iWebContent=3;
	private static int iPage=4;
	private static int iImage=5;
	private static int iBookInfo=6;
	private static int iBookContent=7;
	/**
	 * Create the panel.
	 */
	public WebInformation() {
		setToolTipText("");
		setBorder(new LineBorder(new Color(0, 0, 0)));
		setLayout(null);
		
		link = new JTextField();
		link.setBounds(10, 49, 252, 20);
		add(link);
		link.setColumns(10);
		
		JLabel lblLink = new JLabel("URL");
		lblLink.setBounds(10, 35, 46, 14);
		add(lblLink);
		
		JLabel lblMenu = new JLabel("Menu1");
		lblMenu.setBounds(10, 76, 46, 14);
		add(lblMenu);
		
		menu1 = new JTextField();
		menu1.setColumns(10);
		menu1.setBounds(10, 90, 252, 20);
		add(menu1);
		
		JLabel lblMenu_1 = new JLabel("Menu2");
		lblMenu_1.setBounds(10, 119, 46, 14);
		add(lblMenu_1);
		
		menu2 = new JTextField();
		menu2.setColumns(10);
		menu2.setBounds(10, 133, 252, 20);
		add(menu2);
		
		JLabel lblPagecontent = new JLabel("WebContent");
		lblPagecontent.setBounds(10, 164, 80, 14);
		add(lblPagecontent);
		
		webContent = new JTextField();
		webContent.setColumns(10);
		webContent.setBounds(10, 178, 252, 20);
		add(webContent);
		
		JLabel lblPage = new JLabel("Page");
		lblPage.setBounds(10, 209, 46, 14);
		add(lblPage);
		
		page = new JTextField();
		page.setColumns(10);
		page.setBounds(10, 223, 252, 20);
		add(page);
		
		JLabel lblImage = new JLabel("Image");
		lblImage.setBounds(10, 254, 46, 14);
		add(lblImage);
		
		image = new JTextField();
		image.setColumns(10);
		image.setBounds(10, 268, 252, 20);
		add(image);
		
		JLabel lblBookinfo = new JLabel("BookInfo");
		lblBookinfo.setBounds(10, 299, 46, 14);
		add(lblBookinfo);
		
		bookInfo = new JTextField();
		bookInfo.setColumns(10);
		bookInfo.setBounds(10, 313, 252, 20);
		add(bookInfo);
		
		JLabel lblBookcontent = new JLabel("BookContent");
		lblBookcontent.setBounds(10, 344, 101, 14);
		add(lblBookcontent);
		
		bookContent = new JTextField();
		bookContent.setColumns(10);
		bookContent.setBounds(10, 358, 252, 20);
		add(bookContent);
		
		JLabel lblWebtype = new JLabel("WebType");
		lblWebtype.setBounds(10, 389, 101, 14);
		add(lblWebtype);
		
		webType = new JTextField();
		webType.setColumns(10);
		webType.setBounds(10, 403, 252, 20);
		add(webType);
		
		btnAdd = new JButton("Add");
		btnAdd.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(link.getText().equals("")||menu1.getText().equals("")||link.getText().equals("")||
						webContent.getText().equals("")||page.getText().equals("")||image.getText().equals("")||
						bookInfo.getText().equals("")||webType.getText().equals("")
				)
				{
					JOptionPane.showMessageDialog (null,"Chua dien day du thong tin");
					return;
				}
				else
				{
					Website web= getWebsite();
					WebsiteDAO dao= new WebsiteDAO();
					boolean flag= dao.insert(web);
					if(flag== false)
					{
						JOptionPane.showMessageDialog (null,"Website da ton tai");
						return;
					}
					link.setText("");
					menu1.setText("");
					menu2.setText("");
					webContent.setText("");
					page.setText("");
					image.setText("");
					bookInfo.setText("");
					bookContent.setText("");
					webType.setText("");
					lbID.setText(""); 
					JOptionPane.showMessageDialog (null,"Them webstie "+web.getWebsite()+" thanh cong");
				}	
			}
		});
		btnAdd.setToolTipText("Th\u00EAm website m\u1EDBi v\u00E0o database");
		btnAdd.setBounds(10, 443, 70, 23);
		add(btnAdd);
		
		btnUpdate = new JButton("Update");
		btnUpdate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if(link.getText().equals("")||menu1.getText().equals("")||link.getText().equals("")||
						webContent.getText().equals("")||page.getText().equals("")||image.getText().equals("")||
						bookInfo.getText().equals("")||webType.getText().equals("")
				)
				{
					JOptionPane.showMessageDialog (null,"Chua dien day du thong tin");
					return;
				}
				else
				{
					Website web= getWebsite();
					WebsiteDAO dao= new WebsiteDAO();
					boolean flag= dao.update(web);
					if(flag== false)
					{
						JOptionPane.showMessageDialog (null,"Update website that bai");
						return;
					}
					JOptionPane.showMessageDialog (null,"Update webstie "+web.getWebsite()+" thanh cong");
				}	
			}
		});
		btnUpdate.setToolTipText("Update th\u00F4ng tin website");
		btnUpdate.setBounds(192, 443, 70, 23);
		add(btnUpdate);
		
		lbID = new JLabel("");
		lbID.setBounds(10, 11, 46, 14);
		add(lbID);
		
		JButton btnAddNewWebsite = new JButton("Add new website");
		btnAddNewWebsite.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				link.setText("");
				menu1.setText("");
				menu2.setText("");
				webContent.setText("");
				page.setText("");
				image.setText("");
				bookInfo.setText("");
				bookContent.setText("");
				webType.setText("");
				lbID.setText(""); 
				myWeb=null;
			}
		});
		btnAddNewWebsite.setToolTipText("Th\u00EAm website m\u1EDBi v\u00E0o database");
		btnAddNewWebsite.setBounds(10, 477, 115, 23);
		add(btnAddNewWebsite);
		
		JButton button = new JButton("");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				webGUI.setChoice(iMenu1);
			}
		});
		button.setBounds(272, 90, 21, 21);
		add(button);
		
		btnChoose = new JButton("Choose");
		 btnChoose.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				webGUI.setChoice(0);
			}
		});
		 btnChoose.setBounds(192, 477, 70, 23);
		add(btnChoose);
		
		JButton button_1 = new JButton("");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				webGUI.setChoice(iMenu2);
			}
		});
		button_1.setBounds(272, 132, 21, 21);
		add(button_1);
		
		JButton button_2 = new JButton("");
		button_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				webGUI.setChoice(iWebContent);
			}
		});
		button_2.setBounds(272, 177, 21, 21);
		add(button_2);
		
		JButton button_3 = new JButton("");
		button_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				webGUI.setChoice(iPage);
			}
		});
		button_3.setBounds(272, 222, 21, 21);
		add(button_3);
		
		JButton button_4 = new JButton("");
		button_4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				webGUI.setChoice(iImage);
			}
		});
		button_4.setBounds(270, 267, 21, 21);
		add(button_4);
		
		JButton button_5 = new JButton("");
		button_5.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				webGUI.setChoice(iBookInfo);
			}
		});
		button_5.setBounds(272, 312, 21, 21);
		add(button_5);
		
		JButton button_6 = new JButton("");
		button_6.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				webGUI.setChoice(iBookContent);
			}
		});
		button_6.setBounds(272, 357, 21, 21);
		add(button_6);
	}
	
	public void setWebInformation(Website web)
	{	
		myWeb=web;
		lbID.setText("ID: "+ web.getWebsiteId().toString());
		this.link.setText(web.getWebsite());
		this.menu1.setText(web.getMenu1());
		this.menu2.setText(web.getMenu2());
		this.bookContent.setText(web.getBookContent());
		this.webContent.setText(web.getWebContent());
		this.page.setText(web.getPage());
		this.bookInfo.setText(web.getBookInfo());
		this.image.setText(web.getImage());
		this.webType.setText(web.getWebType());
		
		
	}
	
	public Website getWebsite()
	{
		Website web= new Website();
		if(myWeb!=null)
		{
			myWeb.setWebsite(this.link.getText());
			myWeb.setMenu1(this.menu1.getText());
			myWeb.setMenu2(this.menu2.getText());
			myWeb.setWebContent(this.webContent.getText());
			myWeb.setImage(this.image.getText());
			myWeb.setPage(this.page.getText());
			myWeb.setBookContent(this.bookContent.getText());
			myWeb.setBookInfo(this.bookInfo.getText());
			myWeb.setWebType(webType.getText());
			myWeb.setActive("true");
			web=myWeb;
		}
		else
		{
			web.setWebsite(this.link.getText());
			web.setMenu1(this.menu1.getText());
			web.setMenu2(this.menu2.getText());
			web.setWebContent(this.webContent.getText());
			web.setPage(this.page.getText());
			web.setImage(this.image.getText());
			web.setBookContent(this.bookContent.getText());
			web.setBookInfo(this.bookInfo.getText());
			web.setWebType(webType.getText());
			web.setActive("true");
		}
		return web;		
	}
	
	public void setWebsiteGUI(WebsiteGUI w)
	{
		webGUI=w;
	}
	
	public void setInformation(int n,String t)
	{
		switch(n)
		{
			case 1:
				this.menu1.setText(t);
				break;
			case 2:
				this.menu2.setText(t);
				break;
			case 3:
				this.webContent.setText(t);
				break;
			case 4:
				this.page.setText(t);
				break;
			case 5:
				this.image.setText(t);
				break;
			case 6:
				this.bookInfo.setText(t);
				break;
			case 7:
				this.bookContent.setText(t);			
		}
	}
}
